package com.kye.express.business.wechat_redpacket;

import com.afollestad.materialdialogs.MaterialDialog;
import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.ErrorView;
import com.kye.express.contract.base.view.IView;
import com.kye.express.contract.base.view.LoadingView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.base.RetrofitManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.user.User;
import com.kye.model.bean.wechat.RequestWechatExchangableMoney;
import com.kye.model.bean.wechat.RequestWechatExchange;
import com.kye.model.bean.wechat.RequestWechatOpenIdAddress;
import com.kye.model.bean.wechat.WechatExchangableMoneyBean;
import com.kye.model.bean.wechat.WechatExchangeResultBean;
import com.kye.model.bean.wechat.WechatWechatOpenIdAddressBean;
import com.kye.model.service.WeChatService;

import io.reactivex.Single;
import io.reactivex.SingleSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 09/02/2018
 * @updated : 09/02/2018
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class WeChatPacketExchangeConstract {

    public interface WeChatPacketExchangeView extends IView, LoadingView, ErrorView {

        void onUser(User user);

        void onLoading();

        void onLoaded();

        void getWeChatExchangableMoneySuccess(WechatExchangableMoneyBean wechatExchangableMoneyBean);

        void getWeChatExchangableMoneyNetFailed(String msg);

        void getWeChatExchangableMoneyServerFailed(String message);

        void checkBeforeExchangeSuccess(WechatWechatOpenIdAddressBean wechatWechatOpenIdAddressBean);

        void checkBeforeExchangeNetFailed(String msg);

        void checkBeforeExchangeServerFailed(String message);

        void exchangeSuccess();

        void exchangeServerFailed(String msg);

        void exchangeNetFailed(String message);

    }

    public static class WeChatPacketExchangePresenter extends BasePresenter<WeChatPacketExchangeConstract.WeChatPacketExchangeView> {

        private WeChatService weChatService;

        public WeChatPacketExchangePresenter() {
            weChatService = RetrofitManager.getInstance().createOpenApiService(WeChatService.class);
        }

        @Override
        public void bind(WeChatPacketExchangeView view) {
            super.bind(view);
            UserSession.getInstance()
                    .getUserForRx()
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<User>() {
                        @Override
                        public void accept(User user) throws Exception {
                            getView().onUser(user);
                        }
                    });
        }

        public void getWechatExchangableMoney(User userInfo) {
            Single.just(new RequestWechatExchangableMoney())
                    .doOnSuccess(new Consumer<RequestWechatExchangableMoney>() {
                        @Override
                        public void accept(RequestWechatExchangableMoney requestWechatExchangableMoney) throws Exception {
                            requestWechatExchangableMoney.setPhone(userInfo.getPhone());
                        }
                    })
                    .flatMap(new Function<RequestWechatExchangableMoney, SingleSource<OpenApiResult<WechatExchangableMoneyBean>>>() {
                        @Override
                        public SingleSource<OpenApiResult<WechatExchangableMoneyBean>> apply(RequestWechatExchangableMoney requestWechatExchangableMoney) throws Exception {
                            return weChatService.getWechatExchangableMoney(requestWechatExchangableMoney)
                                    .compose(getView().bindToLifecycleDestroy());
                        }
                    })
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<WechatExchangableMoneyBean>() {
                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onLoaded() {
                            getView().onLoaded();
                        }

                        @Override
                        public void onSuccess(OpenApiResult<WechatExchangableMoneyBean> result, WechatExchangableMoneyBean wechatExchangableMoneyBean) {
                            getView().getWeChatExchangableMoneySuccess(wechatExchangableMoneyBean);
                        }

                        @Override
                        public void onApiError(OpenApiResult<WechatExchangableMoneyBean> result, int code, String msg) {
                            getView().getWeChatExchangableMoneyNetFailed(msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().getWeChatExchangableMoneyServerFailed(e.getMessage());
                        }
                    });
        }

        /**
         * 兑换前判断是否有open id
         * @param userInfo
         */
        public void checkBeforeExchange(User userInfo) {
            Single.just(new RequestWechatOpenIdAddress())
                    .doOnSuccess(new Consumer<RequestWechatOpenIdAddress>() {
                        @Override
                        public void accept(RequestWechatOpenIdAddress requestWechatOpenIdAddress) throws Exception {
                            requestWechatOpenIdAddress.setPhone(userInfo.getPhone());
                        }
                    })
                    .flatMap(new Function<RequestWechatOpenIdAddress, SingleSource<OpenApiResult<WechatWechatOpenIdAddressBean>>>() {
                        @Override
                        public SingleSource<OpenApiResult<WechatWechatOpenIdAddressBean>> apply(RequestWechatOpenIdAddress requestWechatOpenIdAddress) throws Exception {
                            return weChatService.getWechatOpenIdAddress(requestWechatOpenIdAddress)
                                    .compose(getView().bindToLifecycleDestroy());
                        }
                    })
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<WechatWechatOpenIdAddressBean>() {
                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onLoaded() {
                            getView().onLoaded();
                        }

                        @Override
                        public void onSuccess(OpenApiResult<WechatWechatOpenIdAddressBean> result, WechatWechatOpenIdAddressBean wechatWechatOpenIdAddressBean) {
                            getView().checkBeforeExchangeSuccess(wechatWechatOpenIdAddressBean);
                        }

                        @Override
                        public void onApiError(OpenApiResult<WechatWechatOpenIdAddressBean> result, int code, String msg) {
                            getView().checkBeforeExchangeNetFailed(msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().checkBeforeExchangeServerFailed(e.getMessage());
                        }
                    });
        }

        /**
         * 兑换
         * @param userInfo
         * @param money
         */
        public void exchange(User userInfo, String money, String smsCode, MaterialDialog mDialog) {
            Single.just(new RequestWechatExchange())
                    .doOnSuccess(new Consumer<RequestWechatExchange>() {
                        @Override
                        public void accept(RequestWechatExchange requestWechatExchange) throws Exception {
                            requestWechatExchange.setConfirmCode(smsCode);
                            requestWechatExchange.setMoney(money);
                            requestWechatExchange.setPhone(userInfo.getPhone());
                        }
                    })
                    .flatMap(new Function<RequestWechatExchange, SingleSource<OpenApiResult<WechatExchangeResultBean>>>() {
                        @Override
                        public SingleSource<OpenApiResult<WechatExchangeResultBean>> apply(RequestWechatExchange requestWechatExchange) throws Exception {
                            return weChatService.exchange(requestWechatExchange)
                                    .compose(getView().bindToLifecycleDestroy());
                        }
                    })
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<WechatExchangeResultBean>() {
                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onLoaded() {
                            getView().onLoaded();
                        }

                        @Override
                        public void onSuccess(OpenApiResult<WechatExchangeResultBean> result, WechatExchangeResultBean wechatExchangeResultBean) {
                            mDialog.dismiss();
                            getView().exchangeSuccess();
                        }

                        @Override
                        public void onApiError(OpenApiResult<WechatExchangeResultBean> result, int code, String msg) {
                            getView().exchangeServerFailed(msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().exchangeNetFailed(e.getMessage());
                        }
                    });
        }
    }

}
